package com.demo.ctf;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Function;
import java.util.function.Supplier;

public class ThenDemoOrder {
    public static void main(String[] args) throws Exception{
        /**
         * 创建一个线程池，该线程池使用提供的固定数量的线程来执行任务，最多5个线程。
         * 如果所有线程都在工作，那么新提交的任务将在队列中等待，直到有线程变得可用。
         */
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        Supplier<String> task1 = () ->{
            System.out.println("1."+Thread.currentThread().getName());
            return "task1";
        };
        Function<String,String> task2 = s -> {
            System.out.println("2."+Thread.currentThread().getName());
            return "task2";
        };
        /** 创建异步任务*/
        CompletableFuture<String> future = CompletableFuture.supplyAsync(task1,executorService);
        //Thread.sleep(1000);
        /** then* 调用会使用前面任务的线程池 */
        future.thenApply(task2);
        //Thread.sleep(1000);
        System.out.println("haha");
    }
}